package guowei.common.utils;

import lombok.extern.slf4j.Slf4j;
import org.springframework.validation.BindingResult;
import org.springframework.web.bind.MethodArgumentNotValidException;
import org.springframework.web.bind.annotation.ExceptionHandler;
import org.springframework.web.bind.annotation.RestControllerAdvice;

import java.util.HashMap;

@RestControllerAdvice(basePackages = "guowei.guliproduct.product.controller")
@Slf4j
public class GlobalException {

    @ExceptionHandler(value = MethodArgumentNotValidException.class)
    public R handlerException0(MethodArgumentNotValidException methodArgumentNotValidException){
        HashMap<String, String> map = new HashMap<>();
        BindingResult result = methodArgumentNotValidException.getBindingResult();
        result.getFieldErrors().forEach((item)->{
            map.put(item.getDefaultMessage(),item.getField());
        });
        return R.error(400,"提交的数据不合法").put("data",map);
    };

        @ExceptionHandler(value = Exception.class)
    public R handlerException1(Exception exception){
        log.info("数据校验出现问题{},异常类型为{}", exception.getMessage(),exception.getClass());
        return R.error();
    }

}
